/* 
 * Copyright (C) 2012 Martin Řehánek <rehan at mzk.cz>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


goog.provide('kramerius.audio.PlayerMock');

goog.require('goog.Timer');
goog.require('kramerius.audio.Playlist');

/** @const */ var MIN_VOLUME = 0;
/** @const */ var MAX_VOLUME = 100;
/** @const */ var DEFAULT_VOLUME = 50;

/**
 * @constructor 
 */
kramerius.audio.PlayerMock = function(loggingOn){
    this.loggingOn = loggingOn && true;
    console.log("PlayerMock: initializing (logging: " + this.loggingOn + ")" );
    this.playing_ = false;
    this.paused_ = false;
    this.mute_ = false;
    //
    this.trackLength_ = 100;
    this.volume_ = DEFAULT_VOLUME;
    this.setPosition(0.3);
    
    //this.playList_ = new kramerius.audio.Playlist(tracks);
}


kramerius.audio.PlayerMock.prototype.initTimer_ = function(){
    if (this.timer_ != undefined){
        this.timer_.stop();
        this.timer_ = undefined;
    }
    this.timer_ = new goog.Timer(50); //tick each 0.5 second
    goog.events.listen(this.timer_, goog.Timer.TICK, this.incrementPosition_, false, this);
}

kramerius.audio.PlayerMock.prototype.setPosition = function(position){
    this.trackPosition_ = position;
    //console.log("pos: " + position);
    if (this.progressBar_ != undefined){
        this.progressBar_.setPosition(position);
    }
    if (this.progressInfo_ != undefined){
        this.progressInfo_.update(position, this.trackLength_);
    }
    console.log("player position: " + this.trackPosition_);
}

kramerius.audio.PlayerMock.prototype.setTotalLength = function(position){
    //TODO: celkova delka se bude zjistovat dynamicky, jak se budou nacitat data
    //takze se bude muset i menit prepocitivani cas/jednotka delky progressBaru
    //a taky progressInfo
}





kramerius.audio.PlayerMock.prototype.setVolume = function(volume){
    if (volume < MIN_VOLUME || volume > MAX_VOLUME){
        console.log("PlayerMock: Illegal volule level " + volume + ". Ignoring.")
    }else{
        this.volume_ = volume;
        if (this.volumeSlider_ != undefined){
            this.volumeSlider_.setPosition(volume);
        }
        console.log("PlayerMock: volume level=" + volume);    
    }
}


kramerius.audio.PlayerMock.prototype.incrementPosition_ = function(event){
    if (this.trackPosition_ == this.trackLength_){
        this.stop();
    } else{
        //var newPos = this.trackPosition_ + 0.5;
        //console.log("new pos: " + newPos);
        this.setPosition(this.trackPosition_ + 0.5);
    }
}



kramerius.audio.PlayerMock.prototype.play = function(){
    if (this.loggingOn){
        console.log("PlayerMock: playing");
    }
    this.playing_ = true;
    this.initTimer_();
    this.timer_.start();
    this.paused_ = false;
    this.playPauseButton_.setPauseImage();
    this.stopButton_.setEnabledImage();
    this.progressBar_.enable();
}

kramerius.audio.PlayerMock.prototype.pause = function(){
    if (this.loggingOn){
        console.log("PlayerMock: pausing");
    }
    this.playing_ = false;
    this.timer_.stop();
    this.paused_ = true;
    this.playPauseButton_.setPlayImage();
}

kramerius.audio.PlayerMock.prototype.resume = function(){
    if (this.loggingOn){
        console.log("PlayerMock: resuming");
        if(!paused){
            console.log("WARNING: PlayerMock not playing");    
        }
    }
    this.playing_ = true;
    this.timer_.start();
    this.paused_ = false;
    this.playPauseButton_.setPauseImage();
}


kramerius.audio.PlayerMock.prototype.stop = function(){
    if (this.loggingOn){
        console.log("PlayerMock: stopping");
    }
    this.playing_ = false;
    this.initTimer_();
    this.setPosition(0);
    this.paused_ = false;
    this.playPauseButton_.setPlayImage();
    this.stopButton_.setDisabledImage();
    this.progressBar_.disable();
}

kramerius.audio.PlayerMock.prototype.mute = function(){
    this.mute_ = true;
    this.volumeButton_.setDisabledImage();
    this.volumeSlider_.disable();
    //TODO: remove
  //  this.progressBar_.updateClickCounter();
}

kramerius.audio.PlayerMock.prototype.unMute = function(){
    this.mute_ = false;
    this.volumeButton_.setEnabledImage();
    this.volumeSlider_.enable();
    //TODO: remove
 //   this.progressBar_.updateClickCounter();
}

kramerius.audio.PlayerMock.prototype.isPlaying = function(){
    return this.playing_;
}

kramerius.audio.PlayerMock.prototype.isPaused = function(){
    return this.paused_;
}

kramerius.audio.PlayerMock.prototype.isMute = function(){
    return this.mute_;
}

kramerius.audio.PlayerMock.prototype.setPlayPauseButton = function(button){
    this.playPauseButton_ = button;
}

kramerius.audio.PlayerMock.prototype.setStopButton = function(button){
    this.stopButton_ = button;
}

kramerius.audio.PlayerMock.prototype.setVolumeButton = function(button){
    this.volumeButton_ = button;
}

kramerius.audio.PlayerMock.prototype.setProgressBar = function(progressBar){
    this.progressBar_ = progressBar;
}

kramerius.audio.PlayerMock.prototype.setProgressInfo = function(progressInfo){
    this.progressInfo_ = progressInfo;
}


kramerius.audio.PlayerMock.prototype.setVolumeSlider = function(volumeSlider){
    this.volumeSlider_ = volumeSlider;
}




